﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using Factonomy.AzureUtility.Core;

namespace Factonomy.AzureUtility.Dialogs
{
    /// <summary>
    /// Interaction logic for UploadBlob.xaml
    /// </summary>
    public partial class UploadBlob : Window
    {
        private Config _config = new Config();
        public delegate void AddBlobDelegate(AddBlobDetails details);
        public event AddBlobDelegate AddBlob;

        protected void OnAddBlob(AddBlobDetails details)
        {
            if (AddBlob != null)
            {
                AddBlob(details);
            }
        }

        public UploadBlob()
        {
            InitializeComponent();

            this.uiTextboxRoot.Text = this._config.Rootpath.ToLower();
            this.uiLabelPreview.Content = string.Empty;

            this.uiTextboxRoot.TextChanged += new TextChangedEventHandler(UpdatePreview);
            this.uiTextBoxPath.TextChanged += new TextChangedEventHandler(UpdatePreview);
            this.uiRadioFile.Checked += new RoutedEventHandler(RadioClicked);
            this.uiRadioFolder.Checked += new RoutedEventHandler(RadioClicked);
        }

        void RadioClicked(object sender, RoutedEventArgs e)
        {
            this.uiTextBoxPath.Text = string.Empty;
            this.UpdatePreview(null, null);
        }

        void UpdatePreview(object sender, TextChangedEventArgs e)
        {
            this.uiLabelPreview.Content = this.uiTextBoxPath.Text.Replace(this.uiTextboxRoot.Text.ToLower(), "");
        }

        public void CloseWindow(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        public void Browse(object sender, RoutedEventArgs e)
        {
            if ((bool)this.uiRadioFile.IsChecked)
            {
                Microsoft.Win32.OpenFileDialog dialog = new Microsoft.Win32.OpenFileDialog();

                dialog.InitialDirectory = this.uiTextboxRoot.Text;
                dialog.RestoreDirectory = true;

                if (dialog.ShowDialog() == true)
                {
                    this.uiTextBoxPath.Text = dialog.FileName.ToLower();
                    this.UpdatePreview(null, null);
                }
            }

            if ((bool)this.uiRadioFolder.IsChecked)
            {
                using (System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog())
                {
                    dialog.ShowNewFolderButton = false;
                    dialog.RootFolder = Environment.SpecialFolder.MyComputer;
                    dialog.SelectedPath = this.uiTextboxRoot.Text;

                    if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        this.uiTextBoxPath.Text = dialog.SelectedPath.ToLower();
                        this.UpdatePreview(null, null);
                    }
                }
            }
        }

        public void AddSaveButtonClick(object sender, RoutedEventArgs e)
        {
            if ((bool)this.uiRadioFile.IsChecked)
            {
                this.OnAddBlob(new AddBlobDetails(this.uiTextBoxPath.Text, this.uiTextboxRoot.Text, AddBlobDetails.AddActionType.File));
            }

            if ((bool)this.uiRadioFolder.IsChecked)
            {
                this.OnAddBlob(new AddBlobDetails(this.uiTextBoxPath.Text, this.uiTextboxRoot.Text, AddBlobDetails.AddActionType.Folder));
            }

            this.Close();
        }
    }
}
